package student.aistar.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import student.aistar.model.entity.Orders;
import student.aistar.model.entity.WebSeckill;

import java.util.List;
import java.util.Map;

public interface WatchSeckillMapper {

    // 秒杀活动开始之前,加载商品数据
    @Select("select * from web_seckill where startTime>now() and status = '0'")
    List<Map<String,Object>> beforeActive();

    /**
     * 秒杀活动尚未开始 - 秒杀活动进行中
     * 状态{0} 更新成{1}
     */
    @Update("update web_seckill set status = '1' where startTime<now() and now()<endTime and status = '0' ")
    void updateStatus();

    /**
     * 加载出正在进行秒杀的商品的id
     */
    @Select("select id from web_seckill where startTime<now() and now()<endTime and status = '1'")
    List<Long> selectKillId();

    /**
     * 秒杀活动已经结束
     * 状态{1} - {2}
     */
    @Update("update web_seckill set status = '2' where endTime<now() and status = '1'")
    void updateEndStatus();

    /**
     * 加载出秒杀结束后的商品的id
     */
    @Select("select * from web_seckill where endTime<now() and status = '2'")
    List<Long> selectEndKillid();

    /**
     * 插入订单表
     * @param
     * @param
     * @return
     */
    @Options(useGeneratedKeys = true, keyProperty = "orderId", keyColumn = "order_id")
    @Insert("insert into orders values(null,#{orderNumber}, #{userId}, 1, 1, 0, 0, 2, '个人', 1, now(), 599, 599, 1, 1)")
    int insertOrders(Orders order);


    /**
     * 插入订单详情表
     * @param orderId
     */
    @Insert("insert into orderdetail values(null, #{orderId}, 34, 'Xiaomi Watch Color 2', 'https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1632679395.57195055.png', 1, 'Xiaomi Watch Color 2 皓月白', 599, 1, 599, 0)")
    void insertOrderDetail(Integer orderId);


    /**
     * 查询秒杀商品的秒杀时间返回到也没面
     * @param seckillId
     * @return
     */
    @Select("select startTime, endTime from web_seckill where id = #{seckillId}")
    WebSeckill getStartTimeNow(Integer seckillId);
}
